<template>
    <!-- 导出excel组件 -->
    <el-button :type="Type" icon="el-icon-download" size="mini" @click="exportData" >
      <slot></slot>
    </el-button>
</template>

<script>
export default {
  props: {
    excelHeader: Array, // excel 表头
    excelVal: Array, // excel 筛选数据
    excelData: Array, // excel 数据
    excelTitle: String, // excel 文件名
    Type: String // 按钮样式类型
  },
  data () {
    return {}
  },
  methods: {
    // 导出excel
    exportData () {
      // console.log(this.excelData)
      // console.log(this.excelHeader)
      // console.log(this.excelVal)
      // console.log(this.excelTitle)
      if (this.excelData.length <= 0) {
        return this.$message.warning('尚未选中数据！')
      }
      const { export_json_to_excel } = require('../vendor/Export2Excel')
      const tHeader = this.excelHeader // excel表头
      const filterVal = this.excelVal // 所需数据
      const list = this.excelData // 数据
      const data = this.formatJson(filterVal, list)
      export_json_to_excel(tHeader, data, this.excelTitle)
      this.$message.success('正在导出excel，请耐心等待！')
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
